<?php
if ($ticket_parent!=1) {
	header('Location: class_manager.php');
	exit;
}

$result = mysql_query("select pkey from classes", $class_manager_db) or die(mysql_error($class_manager_db));
$num_classes = mysql_num_rows($result);
$per_page=10;
$max_page=(int)(($num_classes-1)/$per_page);
if($max_page<0)
	$max_page=0;

if(isset($_GET["page_nr"]) && intval($_GET["page_nr"])>0 && intval($_GET["page_nr"])<=$max_page){
	$page_nr = intval($_GET["page_nr"]);
}
else if(intval($_GET["page_nr"])>$max_page){
	$page_nr = $max_page;
}
else{
	$page_nr = 0;
}

$prev_page = $page_nr - 1;
if($prev_page < 0)
	$prev_page = 0;

$next_page = $page_nr + 1;
if($next_page > $max_page)
	$next_page = $max_page;
?>

<script type="text/javascript">
	function expand_collapse_div(element_id){
		var t=document.getElementById(element_id);
		if(!t)return false;
		if(t.style.display=="none"){
			t.style.display="block";
		}
		else{
			t.style.display="none";
		}
		return true;
	}

	function status_change(class_pkey,participant_pkey,status){
		//alert(participant_pkey+" "+status.value);

		var status_change_form = document.createElement("form");
		status_change_form.setAttribute("method", "post");
		status_change_form.setAttribute("action", "class_manager.php?page=admin&admin_page=class_lists&page_nr=<?php echo $page_nr;?>&updated="+ class_pkey +"#entry"+ class_pkey);

		var status_change_form_participant_pkey = document.createElement("input");
		status_change_form_participant_pkey.setAttribute("type","hidden");
		status_change_form_participant_pkey.setAttribute("name","status_change_form_participant_pkey");
		status_change_form_participant_pkey.setAttribute("value",participant_pkey);
		status_change_form.appendChild(status_change_form_participant_pkey);

		var status_change_form_status = document.createElement("input");
		status_change_form_status.setAttribute("type","hidden");
		status_change_form_status.setAttribute("name","status_change_form_status");
		status_change_form_status.setAttribute("value",status.value);
		status_change_form.appendChild(status_change_form_status);

		document.body.appendChild(status_change_form);
		status_change_form.submit();

	    return true;
	}

	function popup_close(elementID) {
		document.getElementById(elementID).style.display = "none";

		return true;
	}
	
	function popup_open(elementID) {
		//var leftOffset = (document.width / 2) - 300;

		// Determine how much the visitor had scrolled

		var scrolledX = 0, scrolledY = 0;
		if( self.pageYOffset ) {
		  scrolledX = self.pageXOffset;
		  scrolledY = self.pageYOffset;
		} else if( document.documentElement && document.documentElement.scrollTop ) {
		  scrolledX = document.documentElement.scrollLeft;
		  scrolledY = document.documentElement.scrollTop;
		} else if( document.body ) {
		  scrolledX = document.body.scrollLeft;
		  scrolledY = document.body.scrollTop;
		}

		// Determine the coordinates of the center of the page

		var centerX = 0, centerY = 0;
		if( self.innerHeight ) {
		  centerX = self.innerWidth;
		  centerY = self.innerHeight;
		} else if( document.documentElement && document.documentElement.clientHeight ) {
		  centerX = document.documentElement.clientWidth;
		  centerY = document.documentElement.clientHeight;
		} else if( document.body ) {
		  centerX = document.body.clientWidth;
		  centerY = document.body.clientHeight;
		}

		var leftOffset = scrolledX + (centerX - 600) / 2;
		var topOffset = scrolledY + (centerY - 400) / 2;

		document.getElementById(elementID).style.top = topOffset + "px";
		document.getElementById(elementID).style.left = leftOffset + "px";
		document.getElementById(elementID).style.display = "block";

		return true;
	}

	function open_adjust_payment(div_id, form_name, class_pkey, participant_pkey){
		var adjust_payment_form = document.getElementsByName(form_name)[0];
		adjust_payment_form.setAttribute("action", "class_manager.php?page=admin&admin_page=class_lists&page_nr=<?php echo $page_nr;?>&updated="+class_pkey+"#entry"+class_pkey);
		adjust_payment_form.elements["adjust_payment_form_participant_pkey"].setAttribute("value",participant_pkey);

		
		popup_open(div_id);

		return true;
	}

	function open_payment_details(participant_pkey){
		window.open('payment_details.php?participant='+participant_pkey,'Payment Details','toolbar=no,menubar=no,status=no,directories=no,location=no,width=800,height=600');
		return true;
	}
</script>



<div id="adjust_payment_popup" style="width:600px; display:none; position:absolute;">
	<table class="content" style="width: 600px">
		<tr>
			<td align="center" valign="middle"
				style="background-color: #1C2837; height: 23px; width: 576px; color: #ffffff">Adjust Payment Details</td>
			<td style="background-color: #1C2837"><a
				onclick="popup_close('adjust_payment_popup')" class="clickable"><img
					src="images/close_button.png"
					style="height: 23px; width: 24px; border: 0;"> </a></td>
		</tr>
		<tr>
			<td colspan="2" style="width: 600px;">
				<form name="adjust_payment_form" action="" method="post">
					<table class="form">
						<tr>
							<td align="right">Amount (may be negative):</td>
							<td align="left"><input type="text" style="width: 100px"
								name="adjust_payment_form_amount">
							</td>
						</tr>
						<tr>
							<td valign="top" align="right">Comments:</td>
							<td align="left"><textarea rows="10"
									style="width: 400px" name="adjust_payment_form_comments"></textarea>
							</td>
						</tr>
						<tr>
							<td colspan="2"><input type="hidden"
								name="adjust_payment_form_participant_pkey" value=""> <input
								type="submit" name="adjust_payment_form_submit"
								value="Adjust Payment" />
							</td>
						</tr>
					</table>
				</form>
			</td>
		</tr>
	</table>
</div>







<div class="present">
<h2 class="spaced">Class Lists</h2>
<div class="bspaced">Click a class to expand it for the class list.</div>
<?php 
$start_offset = $page_nr * $per_page;
$class_result = mysql_query("select ordered_classes.pkey, UNIX_TIMESTAMP(ordered_classes.reg_start) as reg_start, ordered_classes.capacity, ordered_classes.fee, ordered_classes.comments, ordered_classes.cancelled, UNIX_TIMESTAMP(ordered_classes.start_datetime) as start_datetime, modules.pkey as module, modules.short_name, modules.long_name from (select * from (select classes.pkey as pkey, classes.module, classes.reg_start, classes.capacity, classes.fee, classes.comments, classes.cancelled, sessions.start_datetime from classes left join sessions on classes.pkey=sessions.class order by start_datetime asc) class_w_startdate group by pkey order by start_datetime desc limit $start_offset, $per_page) ordered_classes left join modules on ordered_classes.module=modules.pkey", $class_manager_db) or die(mysql_error($class_manager_db));
?>
<table>
	<tr>
		<td style="text-align:left;">
			<a title="First Page" href="class_manager.php?page=admin&admin_page=class_lists&page_nr=0">first</a> <a title="Previous Page" href="class_manager.php?page=admin&admin_page=class_lists&page_nr=<?php echo $prev_page;?>">prev</a>
		</td>
		<td style="text-align:center;"></td>
		<td style="text-align:right;">
			<a title="Next Page" href="class_manager.php?page=admin&admin_page=class_lists&page_nr=<?php echo $next_page;?>">next</a> <a title="Last Page" href="class_manager.php?page=admin&admin_page=class_lists&page_nr=<?php echo $max_page;?>">last</a>
		</td>
	</tr>
<?php 
$i=0;
while ($class_line = mysql_fetch_array($class_result, MYSQL_ASSOC)) {
	
	if($class_line["start_datetime"] == NULL)
		$class_header=htmlspecialchars(stripslashes($class_line["long_name"]))." (???)";
	else
		$class_header=htmlspecialchars(stripslashes($class_line["long_name"]))." (".date("d M Y",$class_line["start_datetime"]).")";
?>
	<tr><td colspan="3"><hr></td></tr>
	<tr><td colspan="3">
		<a id="entry<?php echo $class_line["pkey"];?>"></a>
		<h3><a onclick="expand_collapse_div('class_details_<?php echo $i;?>')" class="clickable">
			<?php echo $class_header; if($class_line["cancelled"]==1) echo " - CANCELLED"?>
		</a></h3>
	</td></tr>
	<tr><td colspan="3">
		<div id="class_details_<?php echo $i;?>" <?php if(!isset($_GET["updated"]) || $_GET["updated"] != $class_line["pkey"]) echo 'style="display:none;"';?>>
		
		
		<table>
				<tr>
					<td colspan="2" align="left">
						<form name="match_capacity_form"
							action="class_manager.php?page=admin&admin_page=class_lists&page_nr=<?php echo $page_nr;?>&updated=<?php echo $class_line["pkey"];?>#entry<?php echo $class_line["pkey"];?>"
							method="post">
							<input type="hidden" name="match_capacity_form_class_pkey" value="<?php echo $class_line["pkey"];?>">
							<input type="submit" name="match_capacity_form_submit" value="Match Capacity">
						</form>
					</td>
					<td colspan="2" align="right">
						Fee: <?php echo number_format($class_line["fee"],2);?> ISK
					</td>
				</tr>
		</table>
		<form name="class_list_form"
			action="class_manager.php?page=admin&admin_page=class_lists&page_nr=<?php echo $page_nr;?>&updated=<?php echo $class_line["pkey"];?>#entry<?php echo $class_line["pkey"];?>"
			method="post">
		<table>
				<tr>
					<td colspan="2">
						<h4>Character Name</h4>
					</td>
					<td>
						<h4>Status</h4>
					</td>
					<td>
						<h4>Payment Information</h4>
					</td>
					<td>
						<h4>Registered</h4>
					</td>
				</tr>
<?php 
	$participants_result = mysql_query("select *, UNIX_TIMESTAMP(timestamp) as unix_timestamp from participants where class='".$class_line["pkey"]."' order by alumnus, status asc, character_name", $class_manager_db) or die(mysql_error($class_manager_db));
	while ($participants_line = mysql_fetch_array($participants_result, MYSQL_ASSOC)) {
?>


				<tr>
					<td>
						<?php 
							if($participants_line["alumnus"]==0 && $participants_line["status"]=="registered"){ 
								$checked="";
								$completed_result = mysql_query("select * from completed where name='".$participants_line["character_name"]."' and class='".$participants_line["class"]."'", $class_manager_db) or die(mysql_error($class_manager_db));
								if(mysql_num_rows($completed_result)>0)
									$checked="checked";
								
								echo '<input type="checkbox" name="class_list_form_completed[]" value="'.$participants_line["pkey"].'" '.$checked.'>'; 
								echo '<input type="hidden" name="class_list_form_all[]" value="'.$participants_line["pkey"].'">'; 
							}
						?>
					</td>
					<td>
						<?php echo htmlspecialchars(stripslashes($participants_line["character_name"]));?>
					</td>
					<td>
						<?php if($participants_line["alumnus"]==1) echo "Alumnus, "; ?>
						<select onchange="status_change(<?php echo $class_line["pkey"].", ".$participants_line["pkey"];?>,this);">
							<option value="registered" <?php if($participants_line["status"]=="registered") echo "selected";?> >Registered</option>
							<option value="wait_list" <?php if($participants_line["status"]=="wait_list") echo "selected";?>>Wait Listed</option>
							<option value="withdrawn" <?php if($participants_line["status"]=="withdrawn") echo "selected";?>>Withdrawn</option>
							<option value="kicked" <?php if($participants_line["status"]=="kicked") echo "selected";?>>Kicked</option>
						</select>

					</td>
					<td>
						<?php 
						if($participants_line["alumnus"]==0 && $participants_line["payment_code"]!==NULL){
							$paid_result = mysql_query("select sum(amount) as paid from transactions where payment_code='".$participants_line["payment_code"]."'", $class_manager_db) or die(mysql_error($class_manager_db));
							$paid_line = mysql_fetch_array($paid_result, MYSQL_ASSOC);
							$paid_amount = $paid_line["paid"];
							if($paid_amount>=$class_line["fee"]){
								$paid_class = "green";
							}
							else{
								$paid_class = "red";
							}
							
							echo '<span class="'.$paid_class.'">'.number_format($paid_amount,2)." ISK</span>";
						?>&nbsp;<a title="Adjust Payment Details" onclick="open_adjust_payment('adjust_payment_popup','adjust_payment_form','<?php echo $class_line["pkey"]."', '".$participants_line["pkey"];?>')" class="clickable">adjust</a>&nbsp;<a title="Show Payment Details" onclick="open_payment_details(<?php echo $participants_line["pkey"];?>)" class="clickable">details</a>
						<?php 
						}
						else{
							echo "N/A";
						}
						?>
					</td>
					<td>
						<?php echo date("d M Y, H:i:s",$participants_line["unix_timestamp"]);?>
					</td>
				</tr>

<?php 
	}
?>
			
			</table>
			<input type="submit" name="class_list_form_submit" value="Update Graduated Students" />
			</form>
				
				
				
				
		</div>
	</td></tr>
<?php
	$i++;
}
?>
	<tr><td colspan="3"><hr></td></tr>
	<tr>
		<td style="text-align:left;">
			<a title="First Page" href="class_manager.php?page=admin&admin_page=class_lists&page_nr=0">first</a> <a title="Previous Page" href="class_manager.php?page=admin&admin_page=class_lists&page_nr=<?php echo $prev_page;?>">prev</a>
		</td>
		<td style="text-align:center;"></td>
		<td style="text-align:right;">
			<a title="Next Page" href="class_manager.php?page=admin&admin_page=class_lists&page_nr=<?php echo $next_page;?>">next</a> <a title="Last Page" href="class_manager.php?page=admin&admin_page=class_lists&page_nr=<?php echo $max_page;?>">last</a>
		</td>
	</tr>
</table>
</div>
